package game;

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;


public class Game extends Applet implements Runnable
{

	private static final long serialVersionUID = 1L;
	
	public static final int screenWidth  = 800;
	public static final int screenHeigth = 600;
	static Random rand = new Random();
		
	BufferedImage backbuffer;
	Graphics2D g2d;
	Thread gameLoop;
	static Game instance;	
	Background pause;

	float time;
	
	public void init()
	{
		
		this.backbuffer = new BufferedImage(screenWidth, screenHeigth, BufferedImage.TYPE_INT_RGB);
		this.g2d = backbuffer.createGraphics();
		addKeyListener(Keyboard.getInstance());
		addMouseListener(Mouse.getInstance());
		addMouseMotionListener(Mouse.getInstance());
		//setFocusable(true);
		SceneManager.setup();
	}
	public void start()
	{
		this.gameLoop = new Thread(this);
		this.gameLoop.start();
	}
	public void stop()
	{
		this.gameLoop = null;
	}
	public void run()
	{
		Thread t = Thread.currentThread();

		while (t == this.gameLoop)
		{
			Time.getInstance().UpdateLastTime();
			try 
			{
				Thread.sleep(15);
			}
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
			repaint();
			Time.getInstance().UpdateLoopTime();
		}	
	}
	public void paint(Graphics g)
	{	
		SceneManager.scene.draw(g2d);	
		g.drawImage(backbuffer, 0, 0, this);
	}
	public void update(Graphics g)
	{
		time += Time.getInstance().getLoopTime(); 
		SceneManager.scene.update();
		paint(g);
	}
}


